﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;

namespace Libs.HR
{
    public class Departments
    {
        public int DepartmentID { get; set; }
        public string Name { get; set; }
        public string ShortName { get; set; }
        public string Alias { get; set; }
        public string Description { get; set; }
        public int FatherID { get; set; }
        public int Level { get; set; }
        public int Order { get; set; }
        public string TotalOrder { get; set; }
        public int ReturnValue { get; set; }

        public Departments()
        {

        }

        ~Departments()
        {

        }

        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Lấy giá trị Departments theo ID
        /// </summary>
        /// <param name="departmentID"></param>
        /// <returns></returns>
        public Departments Get(int departmentID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Departments>("sp_Departments_Select",
                new SqlParameter("@DepartmentID", departmentID));
        }

        /// <summary>
        /// Lấy giá trị Departments theo ID
        /// </summary>
        /// <returns></returns>
        public Departments Get()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Departments>("sp_Departments_Select",
                new SqlParameter("@DepartmentID", DepartmentID));
        }

        /// <summary>
        /// Thêm mới Departments
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[7];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@Name", Name);
            pars[2] = new SqlParameter("@ShortName", ShortName);
            pars[3] = new SqlParameter("@Alias", Alias);
            pars[4] = new SqlParameter("@Description", Description);
            pars[5] = new SqlParameter("@FatherID", FatherID);
            pars[6] = new SqlParameter("@Order", Order);

            db.ExecuteNonQuerySP("sp_Departments_Insert", pars);
            DepartmentID = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Cập nhật Contract
        /// </summary>
        public void Update()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[8];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@DepartmentID", DepartmentID);
            pars[2] = new SqlParameter("@Name", Name);
            pars[3] = new SqlParameter("@ShortName", ShortName);
            pars[4] = new SqlParameter("@Alias", Alias);
            pars[5] = new SqlParameter("@Description", Description);
            pars[6] = new SqlParameter("@FatherID", FatherID);
            pars[7] = new SqlParameter("@Order", Order);

            db.ExecuteNonQuerySP("sp_Departments_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        /// <param name="departmentID"></param>
        public void Delete(int departmentID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_Departments_Delete",
                new SqlParameter("@DepartmentID", departmentID));
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        public void Delete()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_Departments_Delete",
                new SqlParameter("@DepartmentID", DepartmentID));
        }

        public DataTable GetTList()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetDataTableSP("sp_Departments_SelectList");
        }

        public List<Departments> GetList()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetListSP<Departments>("sp_Departments_SelectList");
        }

        public List<Departments> GetList(int fatherID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetListSP<Departments>("sp_Departments_SelectListSun",
                new SqlParameter("@FatherID", fatherID));
        }

        public DataTable GetTList(int fatherID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetDataTableSP("sp_Departments_SelectListSun",
                new SqlParameter("@FatherID", fatherID));
        }

    }
}
